"""
Case Type   : 基础功能-兼容B库语法-optimize
Case Name   : optimize mot表
Create At   : 2024/10/28
Owner       : peilinqian
Description :
    step1:创建测试表并插入数据
    step2:创建索引
    step3:删除表中数据
    step4:设置enable_defer_calculate_snapshot为off，optimize表
Expect      :
    step1:创建测试表并插入数据 expect: 创建插入成功
    step2:创建索引 expect: 创建成功
    step3:删除表中数据 expect:成功
    step4:设置enable_defer_calculate_snapshot为off，optimize表 expect:表回收成功
History     :
    modified by @xuwenfei1 2025-3.19:增加---打开MOT开关，enable_mot_server = on
"""
import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)}:初始化----')
        self.pri_sh = CommonSH('PrimaryDbBmode')
        self.constant = Constant()
        self.com = Common()
        self.t_name = 't_compatibility_optimize_0021'

    def test_main(self):
        step_txt = '----查询enable_incremental_checkpoint初始值;----'
        self.log.info(step_txt)
        init_para = self.com.show_param("enable_defer_calculate_snapshot")
        self.init_para1 = self.com.show_param("enable_incremental_checkpoint")
        step_txt = '----step1:关闭增量检查点开关，重启数据库; expect:修改成功----'
        self.log.info(step_txt)
        msg = self.pri_sh.execute_gsguc('set',
                                        self.constant.GSGUC_SUCCESS_MSG,
                                        f'enable_incremental_checkpoint=off')
        self.assertTrue(msg, '执行失败:' + step_txt)
        step_txt = '----打开MOT开关，enable_mot_server = on 重启数据库; expect:修改成功----'
        self.log.info(step_txt)
        msg = self.pri_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG,
                                        f'enable_mot_server = on')
        self.assertTrue(msg, '执行失败:' + step_txt)
        restart = self.pri_sh.restart_db_cluster()
        self.assertTrue(restart, '执行失败:' + step_txt)

        step_txt = '----step2:创建测试表并插入数据 expect: 创建插入成功----'
        self.log.info(step_txt)
        create_sql = f"drop table if exists {self.t_name};" \
                     f"create foreign table {self.t_name} " \
                     f"(c1 int not null,c2 numeric,c3 char(30));" \
                     f"insert into {self.t_name} values" \
                     f"(generate_series(1,1000),50,'testtest');"
        create_result = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(create_result)
        self.assertIn('INSERT', create_result, '执行失败:' + step_txt)

        step_txt = '----step3:创建索引 expect: 创建成功----'
        self.log.info(step_txt)
        create_sql = f"create index test_index on {self.t_name} " \
                     f"using btree(c1);"
        create_result = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(create_result)
        self.assertIn(self.constant.CREATE_INDEX_SUCCESS_MSG, create_result,
                      '执行失败:' + step_txt)

        step_txt = '----step4:删除表中数据 expect:成功----'
        self.log.info(step_txt)
        sql = f'delete from {self.t_name} where c1>800;'
        result = self.pri_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn(self.constant.DELETE_SUCCESS_MSG, result,
                      '执行失败:' + step_txt)

        step_txt = '----step5:设置enable_defer_calculate_snapshot为off，' \
                   'optimize表 expect:表回收成功---'
        self.log.info(step_txt)
        sql = f'alter system set enable_defer_calculate_snapshot = off;' \
              f'select pg_sleep(10);' \
              f'optimize verbose table public.{self.t_name};' \
              f'select pg_sleep(1);' \
              f'optimize verbose table public.{self.t_name};' \
              f'select pg_sleep(1);' \
              f'optimize verbose table public.{self.t_name};' \
              f'alter system set enable_defer_calculate_snapshot={init_para};'
        optimize_result = self.pri_sh.execut_db_sql(sql)
        self.log.info(optimize_result)
        suc_msg = f'table contains 800 rows, 800 rows in sample'
        self.assertEqual(optimize_result.count(suc_msg), 3,
                         '执行失败:' + step_txt)
        self.assertIn(self.constant.VACUUM_SUCCESS_MSG, optimize_result,
                      '执行失败:' + step_txt)
        self.assertEqual(2, optimize_result.count(
            self.constant.alter_system_success_msg), '执行失败:' + step_txt)

    def tearDown(self):
        self.log.info('----this is tearDown----')
        step6_txt = '----step6:清理数据; expect:清理成功----'
        self.log.info(step6_txt)
        drop_sql = f"drop foreign table if exists {self.t_name}; checkpoint;"
        drop_result = self.pri_sh.execut_db_sql(drop_sql)
        self.log.info(drop_result)

        step7_txt = '----还原增量检查点开关，重启数据库生效; expect:成功----'
        self.log.info(step7_txt)
        guc_msg = self.pri_sh.execute_gsguc('set',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            f'enable_incremental_checkpoint'
                                            f'={self.init_para1}')
        guc_msg = self.pri_sh.execute_gsguc('set',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            f'enable_mot_server')
        restart_msg = self.pri_sh.restart_db_cluster()

        self.log.info(f'----{os.path.basename(__file__)}:执行完毕----')
        self.assertIn(self.constant.DROP_FOREIGN_SUCCESS_MSG, drop_result,
                      "执行失败" + step6_txt)
        self.assertIn(self.constant.CHECKPOINT_SUCCESS_MSG, drop_result,
                      "执行失败" + step6_txt)
        self.assertTrue(guc_msg, '执行失败:' + step7_txt)
        self.assertTrue(restart_msg, '执行失败:' + step7_txt)
